package com.gyg.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyg.domain.*;
import com.gyg.dao.CloudRecordMapper;
import com.gyg.enums.FileOperateEnum;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.returns.RespTable;
import com.gyg.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.utils.TimeUtilCustomize;
import com.gyg.vo.PageVO;
import com.gyg.vo.result.CountResult;
import com.gyg.vo.result.RecordResult;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 文件上传下载记录表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
@Slf4j
public class CloudRecordServiceImpl extends ServiceImpl<CloudRecordMapper, CloudRecord> implements CloudRecordService {

    @Autowired
    private CloudPictureService cloudPictureService;

    @Autowired
    private CloudDocumentService cloudDocumentService;

    @Autowired
    private CloudAudioService cloudAudioService;

    @Autowired
    private CloudVideoService cloudVideoService;

    @Override
    public RespTable<List<RecordResult>> cloudRecordList(Integer optionType, Integer page, Integer limit, String username) {

        Integer count = baseMapper.selectRecordCount(optionType, username);

        PageVO pageVO = new PageVO(page, limit, count);
        List<RecordResult> recordResultList = baseMapper.selectRecordList(optionType, pageVO.getBegin() - 1, limit, username);

        //数据映射
        recordResultList = dataMapping(recordResultList);

        return new RespTable<>(RespCode.OK, recordResultList, count);
    }

    private List<RecordResult> dataMapping(List<RecordResult> recordResultList) {
        if (CollectionUtil.isEmpty(recordResultList)) {
            return new ArrayList<>();
        }

        for (RecordResult recordResult : recordResultList) {
            switch (recordResult.getFileType()) {
                case 1 :
                    CloudPicture cloudPicture = cloudPictureService.getById(recordResult.getFileId());
                    recordResult.setFileName(cloudPicture.getPictureName());
                    recordResult.setFileImg(cloudPicture.getPictureUrl());
                    recordResult.setFileByte(cloudPicture.getPictureByte());
                    recordResult.setFileSize(cloudPicture.getPictureSize());
                    recordResult.setFileTime(cloudPicture.getPictureTime());
                    break;
                case 2 :
                    CloudDocument cloudDocument = cloudDocumentService.getById(recordResult.getFileId());
                    recordResult.setFileName(cloudDocument.getDocumentName());
                    recordResult.setFileImg(cloudDocument.getDocumentImg());
                    recordResult.setFileByte(cloudDocument.getDocumentByte());
                    recordResult.setFileSize(cloudDocument.getDocumentSize());
                    recordResult.setFileTime(cloudDocument.getDocumentTime());
                    break;
                case 3 :
                    CloudAudio cloudAudio = cloudAudioService.getById(recordResult.getFileId());
                    recordResult.setFileName(cloudAudio.getAudioName());
                    recordResult.setFileImg(cloudAudio.getAudioImg());
                    recordResult.setFileByte(cloudAudio.getAudioByte());
                    recordResult.setFileSize(cloudAudio.getAudioSize());
                    recordResult.setFileTime(cloudAudio.getAudioTime());
                    break;
                case 4 :
                    CloudVideo cloudVideo = cloudVideoService.getById(recordResult.getFileId());
                    recordResult.setFileName(cloudVideo.getVideoName());
                    recordResult.setFileImg(cloudVideo.getVideoImg());
                    recordResult.setFileByte(cloudVideo.getVideoByte());
                    recordResult.setFileSize(cloudVideo.getVideoSize());
                    recordResult.setFileTime(cloudVideo.getVideoTime());
                    break;
            }
        }
        return recordResultList;
    }

    @Override
    public RespEntity<CountResult> uploadCount() {
        //获取最近七日 含今天
        List<String> recentSevenDayList = TimeUtilCustomize.getRecentSevenDayList();

        CountResult countResult = new CountResult();
        for (int i = 0; i < recentSevenDayList.size(); i++) {
            String date = recentSevenDayList.get(i);
            String begin = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT,
                    TimeUtilCustomize.parseStrToDate(TimeUtilCustomize.DATE_DAY_FORMAT, date));

            QueryWrapper<CloudRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CloudRecord::getFileOperate, FileOperateEnum.UPLOAD.getCode())
                    .eq(CloudRecord::getDeleted, 0)
                    .between(CloudRecord::getRecordTime, begin, date);
            Integer count = baseMapper.selectCount(queryWrapper);

            switch (i) {
                case 0 : countResult.setDays1(count); break;
                case 1 : countResult.setDays2(count); break;
                case 2 : countResult.setDays3(count); break;
                case 3 : countResult.setDays4(count); break;
                case 4 : countResult.setDays5(count); break;
                case 5 : countResult.setDays6(count); break;
                case 6 : countResult.setDays7(count); break;
            }
        }

        //日期格式处理 yyyy-MM-dd HH:mm:ss --> MM-dd
        recentSevenDayList = recentSevenDayList.stream()
                .map(date -> date.split("\\s+")[0].substring(5))
                .collect(Collectors.toList());
        countResult.setDateList(recentSevenDayList);

        return new RespEntity<>(RespCode.SUCCESS, countResult);
    }

    @Override
    public RespEntity<CountResult> downloadCount() {
        //获取最近七日 含今天
        List<String> recentSevenDayList = TimeUtilCustomize.getRecentSevenDayList();

        CountResult countResult = new CountResult();
        for (int i = 0; i < recentSevenDayList.size(); i++) {
            String date = recentSevenDayList.get(i);
            String begin = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT,
                    TimeUtilCustomize.parseStrToDate(TimeUtilCustomize.DATE_DAY_FORMAT, date));

            QueryWrapper<CloudRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CloudRecord::getFileOperate, FileOperateEnum.DOWNLOAD.getCode())
                    .eq(CloudRecord::getDeleted, 0)
                    .between(CloudRecord::getRecordTime, begin, date);
            Integer count = baseMapper.selectCount(queryWrapper);

            switch (i) {
                case 0 : countResult.setDays1(count); break;
                case 1 : countResult.setDays2(count); break;
                case 2 : countResult.setDays3(count); break;
                case 3 : countResult.setDays4(count); break;
                case 4 : countResult.setDays5(count); break;
                case 5 : countResult.setDays6(count); break;
                case 6 : countResult.setDays7(count); break;
            }
        }

        //日期格式处理 yyyy-MM-dd HH:mm:ss --> MM-dd
        recentSevenDayList = recentSevenDayList.stream()
                .map(date -> date.split("\\s+")[0].substring(5))
                .collect(Collectors.toList());
        countResult.setDateList(recentSevenDayList);

        return new RespEntity<>(RespCode.SUCCESS, countResult);
    }
}
